package com.loto.insurance.framework.Air.TK;

import java.text.MessageFormat;
import java.util.Calendar;
import com.loto.insurance.framework.Air.InsAir;
import com.loto.insurance.framework.Air.InsReturnMsg;
import com.loto.insurance.framework.Air.OrderItemInfo;
import com.loto.insurance.framework.config.ConfigManagementAir;
import com.loto.insurance.framework.config.TKConfigAir;
import com.loto.utils.DateUtils;
import com.loto.utils.Http;
import com.loto.utils.MD5;

public class TKInsAir implements InsAir {
  private String productCode;

  private TKConfigAir tkConfigAir;

  public TKInsAir(String productCode) {
	this.productCode = productCode;
	this.tkConfigAir = getTkConfigAir();
  }

  @Override
  public InsReturnMsg PolicyApproval(OrderItemInfo info) {
	InsReturnMsg message = new InsReturnMsg();
	Calendar now = Calendar.getInstance();
	Calendar fromPeriod = now;
	fromPeriod.add(Calendar.MINUTE, 20);

	String postDocument = "<?xml version=''1.0'' encoding=''GB2312'' ?>"
	    + "<消息 功能=''投保'' 机构编号=''{0}'' 操作员编号=''{1}''  流水号=''{2}'' 来源=''上海大古'' >"
	    + "<消息内容 姓名=''{3}'' 证件号=''{4}'' 证件类型=''{5}'' 航班号=''{6}'' 联系电话=''{7}'' 险种编号=''{8}'' 险种名称=''{9}'' 出生日期=''{10}'' 性别='{11}' 保单生效日='{12}' 保单结束日='' 投保时间=''{13}'' 保费=''{14}'' 单证号码='''' 保险期间=''{15}'' 移动电话=''{16}'' 电子邮件=''{17}'' />"
	    + "</消息>";
	postDocument = MessageFormat.format(
	    postDocument,
	    tkConfigAir.getAgencyCode(),
	    tkConfigAir.getOperatorCode(),
	    info.getTransOrderNo(),
	    info.getInsuredName(),
	    info.getInsuredCredentialNo(),
	    info.getInsuredCredentialType(),
	    info.getFlightNo(),
	    info.getInsuredTel(),
	    tkConfigAir.getInsuranceCode(),
	    tkConfigAir.getInsuranceName(),
	    DateUtils.dateToString(info.getInsuredBirth(), "yyyy-MM-dd"),
	    info.getInsuredGender(),
	    info.getFromPeriod().before(now.getTime()) ? DateUtils.dateToString(
	        fromPeriod.getTime(), "yyyy-MM-dd hh:mm:ss") : DateUtils
	        .dateToString(info.getFromPeriod(), "yyyy-MM-dd hh:mm:ss"),
	    DateUtils.dateToString(now.getTime(), "yyyy-MM-dd hh:mm:ss"),
	    tkConfigAir.getPrice(), tkConfigAir.getCycle(), info.getInsuredTel(),
	    info.getInsuredEmail());

	String sendDocument = MD5.GetMD5Code(tkConfigAir
	    .getAgencyVerificationCode() + postDocument);
	postDocument = sendDocument + "|" + postDocument;

	String xmlResponse;
	try {
	  xmlResponse = Http.doXmlPost(tkConfigAir.getPostUrl(), postDocument,
		  "gb2312");
	} catch (Exception e) {
	  // TODO Auto-generated catch block
	  e.printStackTrace();
	}

	/*
	 * try { XmlDocument doc = new XmlDocument(); doc.LoadXml(responseXml);
	 * XmlNode successNode = doc.DocumentElement.SelectSingleNode("//保单信息"); if
	 * (successNode != null) { message.Status = MsgStatus.SUCCESS; message.InsNo
	 * = successNode.Attributes["保单号"].Value; message.InsTransNo =
	 * successNode.Attributes["验证码"].Value; message.InsStatus =
	 * InsuranceStatus.EFFECT; } else { XmlNode failureNode =
	 * doc.DocumentElement.SelectSingleNode("//失败原因"); message.Status =
	 * MsgStatus.FAIL; message.Message = failureNode.InnerXml; }
	 * 
	 * } catch (Exception ex) { message.Status = MsgStatus.FAIL; message.Message
	 * = ex.Message + ex.StackTrace; }
	 */
	return message;
  }

  @Override
  public InsReturnMsg PolicyQuery(OrderItemInfo info) {
	// TODO Auto-generated method stub
	return null;
  }

  @Override
  public InsReturnMsg PolicyCancel(OrderItemInfo info) {
	// TODO Auto-generated method stub
	return null;
  }

  public String getProductCode() {
	return productCode;
  }

  public void setProductCode(String productCode) {
	this.productCode = productCode;
  }

  public TKConfigAir getTkConfigAir() {
	ConfigManagementAir config = new ConfigManagementAir(productCode);
	return config.getTKConfiguration();
  }
}
